package org.cloudie.docs.editor.client.content.dialogs;

import org.cloudie.docs.editor.client.widgets.Dialog;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

/**
 * The basic class for form based dialogs.
 */
public abstract class FormDialog extends Dialog {

  protected static FormDialog instance;

  protected FlexTable content;
  protected Button submit, cancel;
  protected HorizontalPanel buttonPanel;
  
  /**
   * Constructs a New Form dialog.
   */
  protected FormDialog(String title, boolean modal) {
    super(title, modal);
    addClickHandler(new ClickHandler() {
      public void onClick(ClickEvent event) {
        hide();
      }
    });
    submit = new Button("OK");
    cancel = new Button("Cancel");
    buttonPanel = new HorizontalPanel();
    buttonPanel.setSpacing(10);
    buttonPanel.add(submit);
    buttonPanel.add(cancel);
    content = new FlexTable();
    content.setCellPadding(2);
    content.setWidth("500px");
    content.setStylePrimaryName("cloudie-Form");
    setContentWidget(content);
  }
  
  /**
   * Builds the New Icon form.
   */
  protected void buildForm() {
    addField(buttonPanel);
  }

  protected void addField(Widget field) {
	addField(field, null);
  }
  
  protected void addField(Widget field, String name) {
	int i = content.getRowCount();
	if (name == null) {
	  content.insertRow(i);
	  content.insertCell(i, 0);
	  content.getFlexCellFormatter().setColSpan(i, 0, 2);
	  content.setWidget(i, 0, field);
	} else {
	  content.insertRow(i);
	  content.insertCell(i, 0);
	  content.insertCell(i, 1);
	  content.setWidget(i, 0, new Label(name));
	  content.setWidget(i, 1, field);
	}
  }
  
  @Override
  public void hide() {
    super.hide();
  }
  
  public abstract void resetForm();
  
}
